Package Managers
Before defaulting to NPM or Yarn, consider using PNPM.
NPM

NPM Commands
For more commands, see this cheatsheet.
NPM Initialization
npm init- Create a new package.json filenpm i- Can be used as a shortcut fornpm install
NPM Install
npm install- Install all packagesnpm install <package>- Install a packagenpm install <package> --save-dev- Install a package as a dev dependencynpm install <package> --global- Install a package globallynpm uninstall <package>- Uninstall a package
NPM Update and Audit
npm update- Update all packagesnpm outdated- Check for outdated packagesnpm audit- Check for vulnerabilities in installed packagesnpm audit fix- Fix vulnerabilities in installed packagesnpm audit fix --force- Fix vulnerabilities in installed packages and remove outdated packages
NPM Run
npm run <script>- Run a script from the package.json file
Yarn

Yarn Commands
For more commands, see this cheatsheet.
Yarn Initialization
yarn init- Create a new package.json file
Yarn Install
yarn- Install all packages defined in package.json - same asnpm installyarn add <package>- Install a package - same asnpm install <package>yarn add <package> --dev- Install a package as a dev dependency
Yarn Upgrade
yarn upgrade- Update all packages - same asnpm update
Yarn Package Updates
- Link to all documentation
yarn upgrade- Upgrade all packages to their latest versionyarn upgrade left-pad- Upgrade a specific package to its latest versionyarn upgrade left-pad@^1.0.0- Upgrade a specific package to a specific versionyarn upgrade left-pad grunt- Upgrade multiple packages to their latest versionyarn upgrade @angular- Upgrade all packages with the name @angular to their latest versionyarn upgrade --latest- Upgrade all packages to their latest versionyarn upgrade left-pad --latest- Upgrade a specific package to its latest versionyarn upgrade left-pad@^1.0.0 --dev- Upgrade a specific development dependency package to a specific versionyarn remove left-pad- Removes a specific package
PNPM

PNPM Commands
- Link to all documentation
- Introduction article
Switching Package Managers
From NPM to Yarn
> 
- Install yarn using
npm i -g yarnif not already installed - Go to the directory where you install packages and run the
yarncommand - Yarn will init and create its
yarn.lockfile - Now delete
package-lock.json*Note - In your
package.jsonfile replace all npm commands with yarn in "scripts" - Run
yarn devor whatever command you use for running a yarn script
*Note: It is important you don't delete it before yarn command (as some people suggest) it can break things, for example your yarn command will not even work and it will throw error:Error: ENOENT: no such file or directory, open './package-lock.json'
References
From Yarn to PNPM
Migrating from yarn to pnpm is quite straightforward:
-
Install pnpm
npm install -g pnpmif not already installed -
Rename all your yarn commands to pnpm:
yarn->pnpm installyarn test->pnpm testyarn package->pnpm packageyarn deploy->pnpm run deploy(run is required here, aspnpm deployis a reserved command)
-
Replace all occurrences of the string
yarn.lockin your source files withpnpm-lock.yaml(search, prettier, etc.) -
In your CI/CD, when using
actions/setup-node@v3, setcacheto'pnpm' -
If you're using yarn PnP, remove
.yarnrc.ymland the.yarnfolder -
In the root
package.jsonset the packageManager key topnpm@<version>(replace<version>with the latest available version) -
Create a
pnpm-workspace.yamlfile with:packages:
- 'services/*'
- 'contracts/*'
- 'packages/*'and everything that is in the
workspaceskey of the rootpackage.json -
Run
pnpm importto generate apnpm-lock.yamlfrom youryarn.lock, then removeyarn.lock -
Run
pnpm install
References